{include file="public/header" /}
<div class="layui-fluid">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-card-body ">
          <fieldset id="searchFieldset_currentTableRenderId" class="table-search-fieldset">
            <legend>条件搜索</legend>
            <form class="layui-form layui-form-pane form-search">
              <div class="layui-form-item layui-inline">
                <label class="layui-form-label">低库存筛选</label>
                <div class="layui-input-inline">
                  <input type="number" name="sort" placeholder="输入库存查找值" value="{:input('get.sort')}" autocomplete="off"
                    class="layui-input">
                </div>
              </div>
              <div class="layui-form-item layui-inline">
                <label class="layui-form-label">搜索</label>
                <div class="layui-input-inline">
                  <input type="text" name="title" placeholder="商品名称/关键字" value="{:input('get.title')}"
                    autocomplete="off" class="layui-input">
                </div>
              </div>
              <div class="layui-form-item layui-inline">
                <label class="layui-form-label">商品分类</label>
                <div class="layui-input-inline">
                  <select name="classid">
                    <option value="">请选择商品分类</option>
                    {foreach $class as $v}
                    <option value="{$v.id}" {if !empty(input('get.classid')) &&
                      input('get.classid')==$v.id}selected{/if}>
                      {$v.name}</option>
                    {/foreach}
                  </select>
                </div>
              </div>
              <div class="layui-form-item layui-inline">
                <label class="layui-form-label">上下架</label>
                <div class="layui-input-inline">
                  <select name="isSale">
                    <option value=""></option>
                    <option value="1" {if !empty(input('get.isSale')) && input('get.isSale')==1}selected{/if}>上架
                    </option>
                    <option value="2" {if !empty(input('get.isSale')) && input('get.isSale')==2}selected{/if}>下架
                    </option>
                  </select>
                </div>
              </div>
              <div class="layui-form-item layui-inline">
                <input type="hidden" name="ifarmeser" value="{:input('get.ifarmeser')}">
                <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="sreach">搜索</button>
              </div>
            </form>
          </fieldset>
        </div>
        <div class="layui-card-header">
          {if empty(input('get.ifarmeser'))}
          <button class="layui-btn layui-btn-danger layui-btn-sm"
            onclick="delAll('/shop/goods_field','isSale',2)">批量下架</button>
          <button class="layui-btn layui-btn-normal layui-btn-sm"
            onclick="delAll('/shop/goods_field','isSale',1)">批量上架</button>
          <button class="layui-btn layui-btn-danger layui-btn-sm"
            onclick="delAll('/shop/goods_field','isdel',2)">批量删除</button>
          <a title="添加" onclick="open_add('添加','/shop/goods_edit')" href="javascript:;" class="layui-btn layui-btn-sm">
            添加商品
            {/if}
          </a> 数据：<strong id="count">{$list->total()}</strong> 条
        </div>
        <div class="layui-card-body layui-table-body layui-table-main">
          <table class="layui-table layui-form">
            <thead>
              <tr>
                <th>
                  <input type="checkbox" lay-filter="checkall" name="" lay-skin="primary">
                </th>
                <th>
                  <div class="layui-table-cell">ID</div>
                </th>
                <th>
                  <div class="layui-table-cell">商品编号</div>
                </th>
                <th>
                  <div class="layui-table-cell">商品名称</div>
                </th>
                <th>
                  <div class="layui-table-cell">商品图片</div>
                </th>
                <th>
                  <div class="layui-table-cell">分类</div>
                </th>
                <th>
                  <div class="layui-table-cell">单位</div>
                </th>
                <th>
                  <div class="layui-table-cell">订货价</div>
                </th>
                <th>
                  <div class="layui-table-cell">起订量</div>
                </th>
                <th>
                  <div class="layui-table-cell">库存</div>
                </th>
                <th>
                  <div class="layui-table-cell">排序</div>
                </th>
                {if empty(input('get.ifarmeser'))}
                <th>
                  <div class="layui-table-cell">操作</div>
                </th>
                {/if}
              </tr>
            </thead>
            <tbody>
              {foreach $list as $v}
              <tr>
                <td>
                  <input type="checkbox" name="id" value="{$v.id|default='0'}" lay-skin="primary">
                </td>
                <td>
                  <div class="layui-table-cell">{$v.id|default=''}</div>
                </td>
                <td>
                  <div class="layui-table-cell">{$v.goodsn|default=''}</div>
                </td>
                <td>
                  <div class="layui-table-cell">{$v.title|default='--'}</div>
                </td>
                <td>
                  <div id="layer-photos-demo" class="layer-photos-demo">
                    {foreach $v.images as $vs}
                    <img layer-pid="" style="height:50px" layer-src="{$vs}" src="{$vs}" alt="{$v.title}">
                    {/foreach}
                  </div>
                </td>
                <td>
                  <div class="layui-table-cell">{$v.class_name|default='--'}</div>
                </td>
                <td>
                  <div class="layui-table-cell">{$v.unmit|default='--'}</div>
                </td>
                <td>
                  <div class="layui-table-cell">{$v.price|default='--'}</div>
                </td>
                <td>
                  <div class="layui-table-cell">{$v.starbuy|default='--'}{$v.unmit|default='--'}</div>
                </td>
                <td>
                  <div class="layui-table-cell">{$v.stock|default='0'}</div>
                </td>
                <td>
                  <div class="layui-table-cell">{$v.sort|default='0'}</div>
                </td>
                {if empty(input('get.ifarmeser'))}
                <td class="layui-table-col-special">
                  <div class="layui-table-cell">
                    <a title="编辑" onclick="open_add('编辑','/shop/goods_edit?id={$v.id}')" href="javascript:;"
                      class="layui-btn layui-btn-sm">
                      编辑
                    </a>
                    {if $v.isSale == 1}
                    <a title="下架" onclick="open_dels(this,'/shop/goods_field','{$v.id}','isSale',2)" href="javascript:;"
                      class="layui-btn-danger layui-btn layui-btn-sm">
                      下架
                    </a>
                    {else}
                    <a title="显示" onclick="open_dels(this,'/shop/goods_field','{$v.id}','isSale',1)" href="javascript:;"
                      class="layui-btn-normal layui-btn layui-btn-sm">
                      上架
                    </a>
                    {/if}
                    <a title="删除" onclick="open_dels(this,'/shop/goods_field','{$v.id}','isdel',2)" href="javascript:;"
                      class="layui-btn-danger layui-btn layui-btn-sm">
                      删除
                    </a>
                  </div>
                </td>
                {/if}
              </tr>
              {/foreach}
            </tbody>
          </table>
        </div>
        <div class="layui-card-body">
          <div class="page">
            {$list->render()|raw}
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
</body>
{include file="public/footer"}
<script>
  layui.use(['laydate', 'form'], function () {
    var laydate = layui.laydate;
    var form = layui.form;
    // 监听全选
    form.on('checkbox(checkall)', function (data) {
      if (data.elem.checked) {
        $('tbody input').prop('checked', true);
      } else {
        $('tbody input').prop('checked', false);
      }
      form.render('checkbox');
    });
  });
  function delAll2() {
    var ids = [];
    // 获取选中的id 
    $('tbody input').each(function (index, el) {
      if ($(this).prop('checked')) {
        ids.push($(this).val())
      }
    });
    if (ids.length <= 0) {
      layer.msg('请选择商品', { icon: 2, time: 1000 });
      return true;
    }
    return ids;
  }
  function delAll(url, field, status) {
    var ids = [];
    // 获取选中的id 
    $('tbody input').each(function (index, el) {
      if ($(this).prop('checked')) {
        ids.push($(this).val())
      }
    });
    if (ids.length <= 0) {
      layer.msg('请选择删除选项', { icon: 2, time: 1000 });
      return true;
    }
    layer.confirm('确认要删除吗？', function (index) {
      //捉到所有被选中的，发异步进行删除
      $.post(url, { id: ids, field: field, status: status },
        function (data) {
          if (data.status == 0) {
            layer.msg(data.msg, { icon: 1, time: 1000 }, function () {
              // 获得frame索引
              location.reload();
              layer.close(index);
            });
          } else {
            layer.msg(data.msg, { icon: 2, time: 1000 });
          }
        }, "json"
      );
      // $(".layui-form-checked").not('.header').parents('tr').remove();
    });
  }
  function open_dels(obj, url, id, field, status) {
    var ids = [id];
    layer.confirm("确认要执行该操作吗？", function (index) {
      //发异步删除数据
      $.post(
        url,
        { id: ids, field: field, status: status },
        function (data) {
          if (data.status == 0) {
            layer.msg(data.msg, { icon: 1, time: 1000 }, function () {
              // 获得frame索引
              location.reload();
              layer.close(index);
            });
          } else {
            layer.msg(data.msg, { icon: 2, time: 1000 });
          }
        },
        "json"
      );
    });
  }
</script>

</html>